package com.example.acclient;

import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends Activity implements LocationListener {

	private GoogleMap map;
	private LocationManager locationManager;
	private String provider;
	Polyline polyline = null;
	Marker marker;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		provider = LocationManager.GPS_PROVIDER;
		Location lastLocation = locationManager.getLastKnownLocation(provider);

		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();

		if (map != null) {
			// this overrides the initial map type we defined in XML
			map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

			// shows how shapes are used
		}
		marker = map.addMarker(new MarkerOptions().position(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()))
				.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));

		if (lastLocation != null)
			onLocationChanged(lastLocation);
	}

	@Override
	protected void onResume() {
		locationManager.requestLocationUpdates(provider, 5000, 10, this);
		super.onResume();
	}

	@Override
	protected void onPause() {
		locationManager.removeUpdates(this);
		super.onPause();
	}

	public void onLocationChanged(Location location) {
		LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
		addShapes(loc);
		CameraPosition cameraPosition;
		cameraPosition = new CameraPosition.Builder().target(loc) 
				.zoom(15)
				.build();
		map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
		marker.setPosition(loc);
		
	}

	private void addShapes(LatLng ll) {

		// create polyline options that contain points of the line segments
		// that form rectangle.

		if (polyline == null) {
			PolylineOptions polylineOptions = new PolylineOptions();
			polylineOptions.add(ll).color(Color.RED);
			polyline = map.addPolyline(polylineOptions);
		} else {
			List<LatLng> points = polyline.getPoints();
			points.add(ll);
			polyline.setPoints(points);
		}
		// Add a polyline.

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}
}
